﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Ant.ICommon.Utilities
{
    public static class Is
    {
        /// <summary>
        /// 是否空元素
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool Empty<TSource>(IEnumerable<TSource> source)
        {
            return EnumerableHelper.CountOrDefault(source) == 0;
        }

        /// <summary>
        /// 为空的集合，数组中全部不为Null返回true，有一个为Null则返回false。
        /// </summary>
        /// <param name="objs"></param>
        /// <returns></returns>
        public static bool Nulls(params object[] objs)
        {
            return objs.Any(obj => obj == null);
        }

        public static bool Null(object obj)
        {
            return obj == null;
        }

        public static bool NotNull(object obj)
        {
            return !Null(obj);
        }

        public static void NullAction<T>(T instance, Action noNull = null, Action yesNull = null)
        {
            if (instance != null)
            {
                if (Is.NotNull(noNull))
                    noNull();
            }
            else
            {
                if (Is.NotNull(yesNull))
                    yesNull();
            }
        }

        public static TResult NullFunc<TSource, TResult>(TSource instance, Func<TResult> noNull = null, Func<TResult> yesNull = null)
        {
            if (instance != null)
            {
                if (Is.NotNull(noNull))
                    return noNull();
            }
            else
            {
                if (Is.NotNull(yesNull))
                    return yesNull();
            }
            return default(TResult);
        }
    }
}